#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    tabWidget = new QTabWidget(this->centralWidget());
    hexView = new HexView(tabWidget);
    hexView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    tabWidget->setMinimumSize(1200,800);

    tabWidget->addTab(hexView, tr("Test"));

    uchar *buffer = new uchar[21500];
    for(int i = 0; i < 21500; i++)
    {
        buffer[i] = i % 255;
    }
    hexView->loadBytes(buffer, 21500);

    this->showMaximized();
    setupConnectors();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setupConnectors()
{
    connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(slot_openFile()));
    ui->actionCopy->setShortcut(tr("Ctrl+C"));
    connect(ui->actionCopy, SIGNAL(triggered()), hexView, SLOT(copyBytesToClipboard()));
}

void MainWindow::setupSectionWindow(AbstractFileLoader* file)
{
    QList<FileSection*> sections = file->getSections();
    if(sections.isEmpty())
    {
        return;
    }
    //Create a section window if needed
    sectionView = new SectionView(this);
    //Build data model
    SectionModel *sectionModel = new SectionModel();
    sectionModel->addSections(sections);
    sectionView->getTableView()->setModel(sectionModel);
    this->addDockWidget(Qt::RightDockWidgetArea, sectionView);
}

void MainWindow::createNewHexTab(AbstractFileLoader *file)
{
    HexView *tabbedHexView = new HexView(tabWidget);
    tabbedHexView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    tabWidget->setMinimumSize(1200,800);

    tabWidget->addTab(tabbedHexView, tr("Hex Dump"));

    char *fileData = file->getHandle();
    if(fileData == NULL)
    {
        //something wrong
        return;
    }
    tabbedHexView->loadBytes((uchar*)fileData, file->getSize());
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::slot_openFile()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Select executable to open"), QDir::rootPath(), tr("Executable (*.exe *.dll *.sys *.drv)"));
    if (fileName == NULL) return;

    AbstractFileLoader *fileLoader = AbstractFileLoader::getLoader();
    connect(fileLoader, SIGNAL(fileLoaded(AbstractFileLoader*)), this, SLOT(setupSectionWindow(AbstractFileLoader*)));
    connect(fileLoader, SIGNAL(fileLoaded(AbstractFileLoader*)), this, SLOT(createNewHexTab(AbstractFileLoader*)));
    connect(fileLoader, SIGNAL(fileLoaded(AbstractFileLoader*)), this, SLOT(createCodeView(AbstractFileLoader*)));

    fileLoader->load(fileName);
}

void MainWindow::createCodeView(AbstractFileLoader *file)
{
    //get code section
    FileSection *section = file->getSection(".text");
    if(section == NULL)
    {
        //no such section
        return;
    }
    CodeView *codeView = new CodeView(tabWidget);
    tabWidget->addTab(codeView, tr("Code View"));
    codeView->setFont(QFont("Courier New", 11));
    codeView->loadCode((uint)section->getHandle(), section->getSize(), 0);
}

